package com.jxbeidou.system.dict.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.mybatis.service.impl.BaseServiceImpl;
import com.jxbeidou.core.mybatis.convert.PageResultConvert;
import com.jxbeidou.system.dict.convert.DictTypeConvert;
import com.jxbeidou.system.dict.entity.DictTypeDO;
import com.jxbeidou.system.dict.query.DictTypeQuery;
import com.jxbeidou.system.dict.vo.DictTypeVO;
import com.jxbeidou.system.dict.mapper.DictTypeMapper;
import com.jxbeidou.system.dict.service.DictTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 字典类型表
 *
 * @author 杜晓祥 
 * @since 1.0.0 2023-12-14
 */
@Service
@AllArgsConstructor
public class DictTypeServiceImpl extends BaseServiceImpl<DictTypeMapper, DictTypeDO> implements DictTypeService {

    @Override
    public PageResult<DictTypeVO> page(DictTypeQuery query) {
        IPage<DictTypeDO> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        PageResult<DictTypeVO> result = PageResultConvert.convert(page, entityList -> DictTypeConvert.INSTANCE.convertList(entityList));
        return result;
    }

    private LambdaQueryWrapper<DictTypeDO> getWrapper(DictTypeQuery query){
        LambdaQueryWrapper<DictTypeDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StringUtils.isNotEmpty(query.getDictType()), DictTypeDO::getDictType, query.getDictType());
        wrapper.like(StringUtils.isNotEmpty(query.getDictName()), DictTypeDO::getDictName, query.getDictName());
        return wrapper;
    }

    @Override
    public void save(DictTypeVO vo) {
        DictTypeDO entity = DictTypeConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(DictTypeVO vo) {
        DictTypeDO entity = DictTypeConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}